package org.htmlparser.parserapplications.filterbuilder;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Insets;
import javax.swing.Box;
import javax.swing.JPanel;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import org.htmlparser.NodeFilter;
import org.htmlparser.parserapplications.filterbuilder.layouts.VerticalLayoutManager;

public class SubFilterList extends JPanel
{
  protected int mExtra = 25;
  protected Filter mHome;
  protected int mMax;
  protected Component mSpacer;
  protected String mTitle;

  public SubFilterList(Filter paramFilter, String paramString, int paramInt)
  {
    this.mHome = paramFilter;
    this.mTitle = paramString;
    this.mMax = paramInt;
    setLayout(new VerticalLayoutManager());
    addSpacer();
    setSelected(false);
  }

  public void addFilter(Filter paramFilter)
  {
    int i = getComponentCount();
    if (this.mSpacer != null);
    addFilter(paramFilter, --i);
  }

  public void addFilter(Filter paramFilter, int paramInt)
  {
    add(paramFilter, paramInt);
    NodeFilter[] arrayOfNodeFilter1 = this.mHome.getSubNodeFilters();
    NodeFilter[] arrayOfNodeFilter2 = new NodeFilter[1 + arrayOfNodeFilter1.length];
    int i = 0;
    int j = 0;
    if (j < arrayOfNodeFilter2.length)
    {
      Object localObject;
      if (j == paramInt)
        localObject = paramFilter;
      while (true)
      {
        while (true)
        {
          arrayOfNodeFilter2[j] = localObject;
          ++j;
        }
        int k = i + 1;
        localObject = arrayOfNodeFilter1[i];
        i = k;
      }
    }
    this.mHome.setSubNodeFilters(arrayOfNodeFilter2);
    if ((this.mSpacer != null) && (this.mMax != 0) && (arrayOfNodeFilter2.length >= this.mMax))
      removeSpacer();
  }

  protected void addSpacer()
  {
    Dimension localDimension = this.mHome.getSize();
    Insets localInsets = this.mHome.getInsets();
    localDimension.setSize(localDimension.width - localInsets.left - localInsets.right, this.mExtra);
    this.mSpacer = Box.createRigidArea(localDimension);
    add(this.mSpacer);
  }

  public boolean canAccept()
  {
    int j;
    if (this.mMax == 0)
    {
      j = 1;
      label9: return j;
    }
    int i = getComponentCount();
    if (this.mSpacer != null)
      --i;
    if (i < this.mMax)
      j = 1;
    while (true)
    {
      break label9:
      j = 0;
    }
  }

  public Component[] getDropTargets()
  {
    Component[] arrayOfComponent = new Component[1];
    arrayOfComponent[0] = this;
    return arrayOfComponent;
  }

  public Filter[] getFilters()
  {
    NodeFilter[] arrayOfNodeFilter = this.mHome.getSubNodeFilters();
    Filter[] arrayOfFilter = new Filter[arrayOfNodeFilter.length];
    System.arraycopy(arrayOfNodeFilter, 0, arrayOfFilter, 0, arrayOfNodeFilter.length);
    return arrayOfFilter;
  }

  public void removeFilter(int paramInt)
  {
    remove(paramInt);
    NodeFilter[] arrayOfNodeFilter1 = this.mHome.getSubNodeFilters();
    if (arrayOfNodeFilter1.length != 0)
    {
      NodeFilter[] arrayOfNodeFilter2 = new NodeFilter[arrayOfNodeFilter1.length - 1];
      int i = 0;
      for (int j = 0; j < arrayOfNodeFilter1.length; ++j)
        if (j != paramInt)
        {
          int k = i + 1;
          arrayOfNodeFilter2[i] = arrayOfNodeFilter1[j];
          i = k;
        }
      this.mHome.setSubNodeFilters(arrayOfNodeFilter2);
      if ((this.mSpacer == null) && (this.mMax != 0) && (arrayOfNodeFilter2.length < this.mMax))
        addSpacer();
    }
  }

  public void removeFilter(Filter paramFilter)
  {
    Filter[] arrayOfFilter = getFilters();
    int i = -1;
    for (int j = 0; (-1 == i) && (j < arrayOfFilter.length); ++j)
      if (paramFilter == arrayOfFilter[j])
        i = j;
    if (-1 != i)
      removeFilter(i);
  }

  protected void removeSpacer()
  {
    remove(this.mSpacer);
    this.mSpacer = null;
  }

  public void setSelected(boolean paramBoolean)
  {
    if (paramBoolean)
      setBorder(new CompoundBorder(new TitledBorder(null, this.mTitle, 1, 2), new CompoundBorder(new LineBorder(Color.green, 2), new EmptyBorder(1, 1, 1, 1))));
    while (true)
    {
      return;
      setBorder(new CompoundBorder(new TitledBorder(null, this.mTitle, 1, 2), new EmptyBorder(3, 3, 3, 3)));
    }
  }

  public String toString(int paramInt1, int paramInt2)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    Filter[] arrayOfFilter = getFilters();
    for (int i = 0; i < arrayOfFilter.length; ++i)
    {
      localStringBuffer.append(arrayOfFilter[i].toString());
      if (i + 1 != arrayOfFilter.length)
        localStringBuffer.append("\n");
    }
    return localStringBuffer.toString();
  }
}